package com.wqr.mobile.android.ui.utils;

import java.util.List;

import android.util.Log;

import com.google.gson.Gson;

/**
 * json util
 * 
 * @author wenqiurong
 * 
 */
public class JsonUtil {
	private static final String TAG = JsonUtil.class.getSimpleName();
	private static final Gson g = new Gson();

	/**
	 * 注意数据转换异常 it will return null when format error
	 * 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T TransformJson2Vo(String json, Class<T> clazz) {
		T t = null;
		try {
			t = g.fromJson(json, clazz);
		} catch (Exception e) {
			Log.e(TAG, e.getLocalizedMessage());
		}
		return t;
	}

	/**
	 * 
	 * @param list
	 * @return
	 */
	public static <T> String TransformList2Json(List<T> list) {
		String result = "";
		try {
			return result = g.toJson(list);
		} catch (Exception e) {
			Log.e(TAG, e.getLocalizedMessage());
			result = e.getLocalizedMessage();
		}
		return result;
	}

	/**
	 * 
	 * @param s
	 * @return
	 */
	public static String string2json(String s) {
		String result = "";
		try {
			result = g.toJson(s);
		} catch (Exception e) {
			Log.e(TAG, e.getLocalizedMessage());
			result = e.getLocalizedMessage();
		}
		return result;
	}

}
